package appUtils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import com.example.jhlibrarys.R;

/**
 * Created by Administrator on 2017/7/29 0029.
 */

public class PhoneUtil {
    private Activity mActivity;
    public static final int REQUEST_PERMISSION_READ_PHONE_STATE_CODE = 1;
    private String mDeviceId = "";
    private CustomAlertDoubleBtnDialog mCustomAlertDoubleBtnDialog;

    public PhoneUtil(Activity activity) {
        mActivity = activity;
        mCustomAlertDoubleBtnDialog = new CustomAlertDoubleBtnDialog(activity);
    }

    public String getDeviceIDStr() {
        return mDeviceId;
    }

    public void getDevicePermission() {
        if (checkSelfPermission()) {
            TelephonyManager tm = (TelephonyManager) mActivity.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                mDeviceId = tm.getDeviceId();
            } catch (Exception e) {
                e.printStackTrace();
                mDeviceId = "";
                LogUtil.log("deviceId null");
                ToastUtil.showShort(mActivity.getString(R.string.no_read_phone_state_permission_tips));
            }
        } else {
            requestReadPermission();
        }
    }

    private boolean checkSelfPermission() {
        boolean bValue = true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            bValue = mActivity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                    == PackageManager.PERMISSION_GRANTED;
        }
        return bValue;
    }

    public static String getDeviceId(Context context) {
        String deviceId = null;
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            deviceId = tm.getDeviceId();
        } catch (Exception e) {
            LogUtil.log("deviceId null");
            ToastUtil.showShort(context.getString(R.string.no_read_phone_state_permission_tips));
            return deviceId;
        }
        return deviceId;
    }

    /**
     * go SettingActivity to icon_password_open permission
     */
    public void goSetting() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        localIntent.setData(Uri.fromParts("package", mActivity.getPackageName(), null));
        mActivity.startActivity(localIntent);
    }

    private void requestReadPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mActivity.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                    REQUEST_PERMISSION_READ_PHONE_STATE_CODE);
        } else {
            goSetting();
        }
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode != REQUEST_PERMISSION_READ_PHONE_STATE_CODE) {
            return;
        }
        if (grantResults.length > 0) {
            int grantResult = grantResults[0];
            boolean granted = grantResult == PackageManager.PERMISSION_GRANTED;
            boolean showRequestPermission = ActivityCompat.
                    shouldShowRequestPermissionRationale(mActivity, permissions[0]);
            LogUtil.log("onRequestPermissionsResult granted=" + granted);
            if (granted) {
                getDevicePermission();
                return;
            }
            if (showRequestPermission) {//
                getDevicePermission();//checkPermissions again
            } else {
                showPermissionDialog();//已经禁止(动态获取权限 不再询问 被勾选)
            }
        }
    }

    /**
     * 勾选不再提示权限 时的展示对话框
     **/
    public void showPermissionDialog() {
        mCustomAlertDoubleBtnDialog.setTitle(mActivity.getString(R.string.setting));
        mCustomAlertDoubleBtnDialog.setMsg(mActivity.getString(R.string.no_read_phone_state_permission_tips));
        mCustomAlertDoubleBtnDialog.setCancelAble(false);
        CustomAlertDoubleBtnDialog.CancelClickListener cancelClickListener =
                new CustomAlertDoubleBtnDialog.CancelClickListener() {
                    @Override
                    public void onClick() {
                        AppUtil.getInstance().exit();
                    }
                };
        CustomAlertDoubleBtnDialog.commitClickListener commitClickListener =
                new CustomAlertDoubleBtnDialog.commitClickListener() {
                    @Override
                    public void onClick() {
                        goSetting();
                    }
                };
        mCustomAlertDoubleBtnDialog.setCancelClickListener(cancelClickListener);
        mCustomAlertDoubleBtnDialog.setCommitClickListener(commitClickListener);
        mCustomAlertDoubleBtnDialog.showDialog();
    }
}
